//
//  XJLConversationCell.m
//  XJL_WeChat
//
//  Created by XJL on 16/8/16.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "XJLConversationCell.h"

@interface XJLConversationCell()

@property (weak, nonatomic) IBOutlet UIImageView *huddyIconImg;
@property (weak, nonatomic) IBOutlet UILabel *huddyTitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *huddySubtitleLabel;
@property (weak, nonatomic) IBOutlet UILabel *contactTimeLabel;
//未读消息数
@property (weak, nonatomic) IBOutlet UILabel *myUnreadCountLabel;

@end
@implementation XJLConversationCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)setConversation:(EMConversation *)conversation{
    _conversation = conversation;
    // 1.显示用户名
    self.huddyTitleLabel.text = conversation.chatter;
    // 最新的聊天消息
    EMMessage *lastMessage = conversation.latestMessage;
     // 2.显示时间
    if (lastMessage) {
        NSString *lastMessageTime = [NSDate formattedTimeFromTimeInterval:lastMessage.timestamp];
        self.contactTimeLabel.text = lastMessageTime;
        
    }
    
    //3.显示最后一条的内容
    NSString *latestMessageTitle = nil;
    if (lastMessage) {
        id<IEMMessageBody> messageBody = lastMessage.messageBodies.lastObject;
        switch (messageBody.messageBodyType) {
            case eMessageBodyType_Image:{
                latestMessageTitle = NSLocalizedString(@"message.image1", @"[image]");
            } break;
            case eMessageBodyType_Text:{
                // 表情映射
                NSString *didReceiveText = [EaseConvertToCommonEmoticonsHelper
                                            convertToSystemEmoticons:((EMTextMessageBody *)messageBody).text];
                latestMessageTitle = didReceiveText;
            } break;
            case eMessageBodyType_Voice:{
                latestMessageTitle = NSLocalizedString(@"message.voice1", @"[voice]");
            } break;
            case eMessageBodyType_Location: {
                latestMessageTitle = NSLocalizedString(@"message.location1", @"[location]");
            } break;
            case eMessageBodyType_Video: {
                latestMessageTitle = NSLocalizedString(@"message.video1", @"[video]");
            } break;
            case eMessageBodyType_File: {
                latestMessageTitle = NSLocalizedString(@"message.file1", @"[file]");
            } break;
            default: {
            } break;
        }
    }
    self.huddySubtitleLabel.text = latestMessageTitle;
    
    // 4.显示未读消息数
    self.myUnreadCountLabel.text = [NSString stringWithFormat:@"%zd",[conversation unreadMessagesCount]];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
